package org.cqut.system.class_course.controller;

import org.cqut.framework.web.response.ResultInfo;
import org.cqut.framework.web.response.Status;
import org.cqut.system.class_course.domain.ClassCourse;
import org.cqut.system.class_course.service.ClassCourseService;
import org.cqut.system.class_course.vo.ClassListVo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XiAoXu
 * @date 2021/11/11 17:22
 * @description
 */
@RestController
    @RequestMapping("/classCourse")
public class ClassCourseController {
    private final ClassCourseService classCourseService;

    public ClassCourseController(ClassCourseService classCourseService) {
        this.classCourseService = classCourseService;
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResultInfo saveRecord(@RequestBody ClassCourse classCourse) {
        boolean flag = classCourseService.save(classCourse);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }

    @RequestMapping(method = RequestMethod.POST,path = "/remove")
    public ResultInfo removeRecord(@RequestBody ClassCourse classCourse) {
        boolean flag = classCourseService.removeByClassIdAndCourseId(classCourse);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }

    @RequestMapping(method = RequestMethod.GET, path = "/list/{courseId}")
    public ResultInfo queryRecord(@PathVariable Long courseId) {
        List<ClassListVo> classListVos = classCourseService.getRecordByCourseId(courseId);
        if (classListVos != null) {
            return new ResultInfo(Status.QUERY_DETAILS_SUCCESS.code, Status.QUERY_DETAILS_SUCCESS.message, classListVos);
        } else {
            return new ResultInfo(Status.QUERY_DETAILS_ERROR.code, Status.QUERY_DETAILS_ERROR.message);
        }
    }
}
